package com.terrace.core.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * Jackson工具类
 * 
 * @author jiangyg
 *
 */
public class JsonUtils {

	private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);

	private static ObjectMapper mapper;

	/**
	 * 获取ObjectMapper实例
	 * 
	 * @param createNew
	 *            方式：true，新实例；false,存在的mapper实例
	 * @return
	 */
	public static ObjectMapper getMapperInstance(boolean createNew) {
		if (createNew) {
			return createObjectMapper();
		} else if (mapper == null) {
			synchronized (JsonUtils.class) {
				mapper = createObjectMapper();
			}
		}
		return mapper;
	}

	private static ObjectMapper createObjectMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		return objectMapper;
	}

	/**
	 * 将java对象转换成json字符串
	 * 
	 * @param obj
	 *            准备转换的对象
	 * @return json字符串
	 * @throws Exception
	 */
	public static String beanToJson(Object obj) {
		String json = null;
		try {
			ObjectMapper objectMapper = getMapperInstance(false);
			json = objectMapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.error("json字符串转javaBean出错：", e);
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 将java对象转换成json字符串
	 * 
	 * @param obj
	 *            准备转换的对象
	 * @param createNew
	 *            ObjectMapper实例方式:true，新实例;false,存在的mapper实例
	 * @return json字符串
	 */
	public static String beanToJson(Object obj, Boolean createNew) {
		String json = null;
		try {
			ObjectMapper objectMapper = getMapperInstance(createNew);
			json = objectMapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.error("javaBean转json字符串出错：", e);
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 将json字符串转换成java对象
	 * 
	 * @param json
	 *            准备转换的json字符串
	 * @param clazz
	 *            准备转换的类
	 * @return
	 * @throws IOException
	 */
	public static <T> T jsonToBean(String json, Class<T> clazz) {
		T t = null;
		try {
			ObjectMapper objectMapper = getMapperInstance(false);
			t = objectMapper.readValue(json, clazz);
		} catch (IOException e) {
			logger.error("json字符串转javaBean出错：", e);
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * 将json字符串转换成java对象
	 * 
	 * @param json
	 *            准备转换的json字符串
	 * @param clazz
	 *            准备转换的类
	 * @param createNew
	 *            ObjectMapper实例方式:true，新实例;false,存在的mapper实例
	 * @return
	 * @throws IOException
	 */
	public static <T> T jsonToBean(String json, Class<T> clazz, Boolean createNew) {
		T t = null;
		try {
			ObjectMapper objectMapper = getMapperInstance(createNew);
			t = objectMapper.readValue(json, clazz);
		} catch (IOException e) {
			logger.error("json字符串转javaBean出错：", e);
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * json字符串转集合
	 * 
	 * @param json
	 * @param typeRef
	 * @return
	 * @throws IOException
	 */
	public static <T> T jsonToBean(String json, TypeReference<T> typeRef) throws IOException {
		T t = null;
		try {
			ObjectMapper objectMapper = getMapperInstance(false);
			t = objectMapper.readValue(json, typeRef);
		} catch (IOException e) {
			logger.error("json字符串转集合出错：", e);
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * json字符串转集合
	 * 
	 * @param json
	 * @param typeRef
	 * @param createNew
	 * @return
	 * @throws IOException
	 */
	public static <T> T jsonToBean(String json, TypeReference<T> typeRef, Boolean createNew) throws IOException {
		T t = null;
		try {
			ObjectMapper objectMapper = getMapperInstance(createNew);
			t = objectMapper.readValue(json, typeRef);
		} catch (IOException e) {
			logger.error("json字符串转集合出错：", e);
			e.printStackTrace();
		}
		return t;
	}

}